package com.standardoperationapp.materialinventory.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.standardoperationapp.R;
import com.standardoperationapp.global.GlobalVaries;
import com.standardoperationapp.global.ListViewItem;

import java.util.ArrayList;
import java.util.List;

public class DetailListAdapter extends BaseAdapter {

    //	private List<MessageItem>nlist =new ArrayList<MessageItem>();
	/*private int resourceId;
	public MsgAdapter(Context context, int resource,List<MessageItem> objects) {
		super(context, resource, objects);
		// TODO Auto-generated constructor stub
		resourceId=resource;
	}*/
    private List<ListViewItem> list;
    private LayoutInflater mInflater;

    public DetailListAdapter(Context context, List<ListViewItem> list1) {
        list = list1;
        mInflater = LayoutInflater.from(context);
    }


    public int getCount() {
        return list.size();
    }

    public Object getItem(int position) {
        return list.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public int getItemViewType(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent)
    {
        ListViewItem item = list.get(position);
        ViewHolder viewHolder = null;

        if(convertView == null){
            convertView = mInflater.inflate(R.layout.listview_item, null);
            viewHolder=new ViewHolder(
                    (View) convertView.findViewById(R.id.list_child),
                    (TextView) convertView.findViewById(R.id.notxt),
                    (TextView) convertView.findViewById(R.id.positiontxt),
                    (TextView) convertView.findViewById(R.id.nametxt),
                    (TextView) convertView.findViewById(R.id.tv_type),
                    (TextView) convertView.findViewById(R.id.codetxt),
                    (TextView) convertView.findViewById(R.id.numbertxt),
                    (TextView) convertView.findViewById(R.id.scannumbertxt),
                    (TextView) convertView.findViewById(R.id.infotxt)
            );
            convertView.setTag(viewHolder);
        }
        else{
            viewHolder = (ViewHolder)convertView.getTag();
        }

        viewHolder.noTxt.setText("" + (position+1));
        viewHolder.positionTxt.setText(item.getPosition());
        viewHolder.nameTxt.setText(item.getName());
        viewHolder.numberTxt.setText("" + item.getNumber());
//        viewHolder.infoTxt.setText(item.getInfo());
        if ((position % 2) == 1) {
            viewHolder.child.setBackgroundColor(Color.parseColor("#F1F1F1"));
        } else {
            viewHolder.child.setBackgroundColor(Color.WHITE);
        }

        if (GlobalVaries.uiType== GlobalVaries.XIANCHANGWUZI) {
            viewHolder.typeTxt.setVisibility(View.VISIBLE);
            viewHolder.typeTxt.setText(item.getType());
            viewHolder.codeTxt.setVisibility(View.GONE);
            viewHolder.scannoTxt.setVisibility(View.GONE);
        } else if (GlobalVaries.uiType== GlobalVaries.PANDIAN) {
            viewHolder.typeTxt.setVisibility(View.VISIBLE);
            viewHolder.typeTxt.setText(item.getType());
            viewHolder.codeTxt.setText(item.getCode());
            viewHolder.scannoTxt.setText("" + item.getScanno());
        } else {
            viewHolder.codeTxt.setText(item.getCode());
            viewHolder.scannoTxt.setText("" + item.getScanno());
        }

//        {
//            viewHolder.child.setBackgroundResource(R.drawable.msgbox_send);
//            viewHolder.msgTextView.setText(item.getComItem());
//        }
//        else
//        {
//            viewHolder.child.setBackgroundResource(R.drawable.msgbox_rec);
//            viewHolder.msgTextView.setText(item.getComItem());
//        }


        return convertView;
    }
    class ViewHolder
    {
        protected TextView noTxt,positionTxt,nameTxt,codeTxt,numberTxt,scannoTxt,infoTxt,typeTxt;
        protected View child;


        public ViewHolder(View child, TextView no,TextView position,TextView name,TextView type,TextView code,
                          TextView number,TextView scanno,TextView info){
            this.child = child;
            this.noTxt = no;
            this.codeTxt =code;
            this.nameTxt = name;
            this.typeTxt = type;
            this.positionTxt=position;
            this.numberTxt = number;
            this.scannoTxt = scanno;
            this.infoTxt = info;


        }
    }
}

